//
//  CustomerTestView.m
//  HuoBao
//
//  Created by sky on 15/12/1.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CustomerTestView.h"

@interface CustomerTestView ()

@property (nonatomic, strong) UIView *dangban;
@property (nonatomic, strong) UIView *bgview;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger num;

@end

@implementation CustomerTestView

- (instancetype)initWithContent:(NSString *)cont TrueOrnot:(BOOL)Select{
    self = [super init];
    self.num = 0;
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    self.dangban = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.dangban.backgroundColor = [UIColor blackColor];
    self.dangban.alpha = 0.3;
    [self addSubview:self.dangban];
    
    UIImage *right = [UIImage imageNamed:@"duihao_40"];
    UIImage *wrong = [UIImage imageNamed:@"alter_wrong"];
    
    
    CGSize strsize = [cont sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    self.bgview = [[UIView alloc]init];
    
    UIImageView *alt = [[UIImageView alloc]init];
    UILabel *contlbl = [[UILabel alloc]init];
    
    double hang = strsize.width / 180;
    if ( hang < 1 || hang == 1) {
        
        self.bgview.frame = CGRectMake(SCREEN_WIDTH/2 - (strsize.width + right.size.width + 6 + 42)/2 , SCREEN_HEIGHT/2 - 336/4 - 25, strsize.width + right.size.width + 6 + 42, strsize.height + 60);
        
        alt.frame = CGRectMake(self.bgview.width/2 - (strsize.width + right.size.width + 6)/2, self.bgview.height/2 - right.size.height/2, right.size.width, right.size.height);
        
        contlbl.frame = CGRectMake(alt.origin.x + right.size.width + 6, alt.origin.y + 2, strsize.width, 15);
        
    }else if(hang > 1){
        
        self.bgview.frame = CGRectMake(SCREEN_WIDTH/2 - (180 + right.size.width + 6 + 42)/2 , SCREEN_HEIGHT/2 - 336/4 - 25, 180 + right.size.width + 6 + 42, right.size.height * 2 + 60);
        
        alt.frame = CGRectMake(self.bgview.width/2 - (180 + right.size.width + 6)/2, 58/2 , right.size.width, right.size.height);
        
        contlbl.frame = CGRectMake(alt.origin.x + right.size.width + 6, alt.origin.y , 180, 38);
        
    }
    
    self.bgview.backgroundColor = [LCCommon getColor:@"3d3e41"];
    self.bgview.alpha = 0;
    self.bgview.layer.cornerRadius = 4;
    [self addSubview:self.bgview];
    
    [self.bgview addSubview:alt];
    
    contlbl.text = cont;
    contlbl.font = [UIFont systemFontOfSize:15];
    contlbl.numberOfLines = 0;
    contlbl.textColor = [LCCommon getColor:@"b1b1b1"];
    [self.bgview addSubview:contlbl];
    
    
    if (Select) {
        alt.image = right;
    }else{
        alt.image = wrong;
    }
    return self;
}

- (void)showInView{
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        self.bgview.backgroundColor = [LCCommon getColor:@"3d3e41"];
        self.bgview.alpha = 1;
    }completion:^(BOOL finished) {
        self.timer =[NSTimer scheduledTimerWithTimeInterval:0.2
                                                     target:self
                                                   selector:@selector(getResults)
                                                   userInfo:nil
                                                    repeats:YES];
    }];
}


-(void)getResults{
    LLog(@"getResults");
    if (self.num > 5) {
        [self.timer invalidate];
        [self removeFromSuperview];
        return;
    }
    self.num ++;
}

-(UILabel *)LabelWithRect:(CGRect)rect textColor:(UIColor *)color textstr:(NSString *)str textfont:(int)font textstyle:(NSTextAlignment)aligment{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textColor = color;
    label.text = str;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = aligment;
    label.numberOfLines = 0;
    return label;
}



@end
